<?php 
$path = drupal_get_path('module', 'circulars'); 
drupal_add_css($path . '/css/circulars.css');

/**
 * Function for full_circulars()
 * @return string
 */
function full_circulars(){

	 $dep_val = arg(2); 

	db_set_active('cmsdb');
	
	$op = get_circulars_header('dept');
	$op .= "<div id='result_go' class='result_inner'>";
	$css = '/css/circulars.css';
	$module_path = drupal_get_path('module', 'circulars');
		
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
		->entityCondition('bundle', 'notification')
		->propertyCondition('status',1)
		->range(0,20);
	$cir_result = $query->execute();
	
	if (isset($cir_result['node'])) {
		$news_items_nids = array_keys($cir_result['node']);
	}
	db_set_active('default');
	
	foreach($news_items_nids as $nid){
		my_db_change(1);
		$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
		$res = node_load($nid, $vid);
		my_db_change(0);
		
		$filepath = $res->field_not_upload['und'][0]['uri'];
		$filesize = $res->field_not_upload['und'][0]['filesize'];
		$title = $res->title;
		if ($res->field_not_upload['und'][0]['filesize'] != '0') {
			$kb = ceil($res->field_not_upload['und'][0]['filesize']/1024);
			$kbyte= $kb.KB;
			if($kb >1024) {
				$kb = ceil($kb/1024);
				$kbyte= $kb.MB;
			}
		}
		if($res->field_not_availabilty['und'][0]['value'] == 'download')  {
			$link = $res->field_not_upload['und'][0]['uri'];
			$wrapper = file_stream_wrapper_get_instance_by_uri($link);
			if($wrapper == true){
				$pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
			}
			$filepath="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
			$icon = '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">';
			$icon = $icon. $kbyte;
		}
		else  {
			$filepath = $res->field_not_url['und'][0]['value'];
			$filepath ="'$filepath'";
			$icon = ''	;
		}		
		
		$op1 .= '<div class="go_view_dept_list"> <p>
		
		<a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepath.');">'.$title .'</a>' . $icon. '</p>
		
			</div>';
						
		}
		
		$output .= $op;
		
		if($res){
				$output .= $op1;
		}else {
				$output .='<div class="no_record">No Results Found</div>';
		}
		//$output .= "<div class='clr'></div></div>";
		
		return $output.theme('pager');
}

function circulars_department(){

	global $base_url,$language;
	
	db_set_active('cmsdb');
	
	$op = get_circulars_header( 'dept' ) ."</div>";	
	$op .= "<div id='result_go' class='result_inner'>";
	//print_r($result);
	$qry = db_select('depart_mst','d');
		$qry->fields('d',array('dep_id','dept_name'))->distinct();
		$qry->addJoin('INNER','field_data_field_not_department','cd', 'd.dep_id = CAST(cd.field_not_department_value AS INT)');
		$qry->condition('n.status', 1);
		$qry->addJoin('INNER','node','n','n.nid = cd.entity_id' );
		$qry->orderby('d.dep_id','ASC');
		
	//print $qry;
	$result = $qry->execute();	
	db_set_active('default');
	
	foreach($result as $res){
	
			if($language->language == 'en'){
			 	$op .= "<div class='go_view_dept_list'> <p>" .l(t($res->dept_name), 'circulars/dept/' . $res->dep_id) . "</p> </div>" ; 
			}else{
				$op .= "<div class='go_view_dept_list'> <p>" .l(t($res->dept_name), 'ta/circulars/dept/' . $res->dep_id) . "</p> </div>" ; 
			}

	}
	
	$output .= $op;
	$output .= "<div class='clr'></div>";	
	
	
	return $output;
}

function circulars_department_details(){
	
	global $language,$base_url;
	 $dep_val = arg(2); 
	//print_r($language);
		db_set_active('cmsdb');
		//drupal_add_css(drupal_get_path('module', 'circulars') . 'css/circulars.css');
		$op =  get_circulars_header( 'dept' );
		$op .= "<div id='result_go' class='result_inner'>";
		$op .= "<div class='go_view'><div class='back_btn'><span>" .l(t('Back'),'circulars/dept'). "</span></div>";	
		//print_r($result);
			/*$qry = db_select('node','n');
				$qry->addField('n','title');
				$qry->addJoin('INNER','field_data_field_not_availabilty','ca', 'n.nid = ca.entity_id');
				$qry->addJoin('INNER','field_data_field_not_department','cd', 'n.nid = cd.entity_id');
				$qry->addJoin('INNER','depart_mst','d','d.dep_id = CAST(cd.field_not_department_value AS INT)');
				$qry->addJoin('INNER','field_data_field_not_upload','cf','n.nid = cf.entity_id');
				$qry->addJoin('INNER','file_usage','fu','fu.id = n.nid');
				$qry->addJoin('INNER','file_managed','fm','fm.fid = fu.fid');
				$qry->addField('fm','uri');
				$qry->addField('fm','filesize');
				$qry->condition('n.status', '1');
				$qry->condition('d.dep_id', $dep_val,'=');
				
				
				
				$result=$qry->extend('PagerDefault')->limit(20)->execute();*/
				
				$query = new EntityFieldQuery();
				$query->entityCondition('entity_type', 'node')
					->entityCondition('bundle', 'notification')
					->propertyCondition('status',1)
					->fieldCondition('field_not_department', 'value', $dep_val, '=')
					->fieldOrderBy('field_not_department', 'value', 'DESC')
					->pager(25,0); 
			
				$result = $query->execute();
				 
				$nid = current($result['node'])->nid;
				 
				if (isset($result['node'])) {
					$news_items_nids = array_keys($result['node']);
					//$news_items = entity_load('node', $news_items_nids);
					$news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
				}
			
		db_set_active('default');
		
		foreach($news_items_nids as $nid) {
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$res = node_load($nid, $vid);
			my_db_change(0);
		 
		// print "<pre>"; print_r($res); print "</pre>";
			
		
		$filepath = $res->field_not_upload['und'][0]['uri'];
		$filesize = $res->field_not_upload['und'][0]['filesize'];
		$title = $res->title;
		if ($res->field_not_upload['und'][0]['filesize'] != '0') {
						$kb = ceil($res->field_not_upload['und'][0]['filesize']/1024);
						$kbyte= $kb.KB;
						if($kb >1024) { 
							$kb = ceil($kb/1024);
							$kbyte= $kb.MB;
						}
					}
		if($res->field_not_availabilty['und'][0]['value'] == 'download')  {
				 $link = $res->field_not_upload['und'][0]['uri'];		
				$wrapper = file_stream_wrapper_get_instance_by_uri($link);
				if($wrapper == true){
					 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
				}
			 	$filepath="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
				$icon = '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">'; 
				$icon = $icon. $kbyte;
			}
			else  {
				$filepath = $res->field_not_url['und'][0]['value'];
				$filepath ="'$filepath'";
				$icon = ''	; 
			}
					

		
		/*if( $goYear != $go_year )		{ 
					$go_year = $goYear;
					$divOutput1 = "<div class='res_dept go_depart' ><strong>Year : " .  $goYear  . "</strong></div>";
				}
				else		{
					$divOutput1 = '';
				}
		
		if( $deptName != $curdept )		{
			$curdept = $deptName;
			$divOutput =   t(ucfirst($deptName)) ;
						
		}
		else{
			$divOutput = '';
		}
	*/
 
	
	$op1 .= '<div class="go_view_dept_list"> <p>
				
				<a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepath.');">'.$title .'</a>' . $icon. '</p>
				  
			</div>';
			
	}
	$output .= $op;
	if($res){
		$output .= $op1;
	}else {
		$output .='<div class="no_record">No Results Found</div>';
	}
	
		//$output .= "<div class='clr'></div></div>";	
		
		return $output.theme('pager');
				
}
function Cir_atoz_letter($sel){

	global $base_url,$language;

	drupal_set_title(t('Government of Tamil Nadu')." : ".t('Circulars and notofication'));
	
	$listArr = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "All" );
	foreach( $listArr as $key => $val )	{
		if( $sel == $val || strtolower( $sel ) == strtolower( $val ) )		{
			$selected = 'view_head_sel';
				
		}else{
			$selected = 'view_head_nonsel';
			
		}
		if($language->language == 'en'){
			$linkOp .= "<a href='" . $base_url . "/circulars/atoz/" . $val . "' class='" . $selected . "'>" . $val . "</a>&nbsp;&nbsp;";
		}
		else{
			$linkOp .= "<a href='" . $base_url . "/ta/circulars/atoz/" . $val . "' class='" . $selected . "'>" . $val . "</a>&nbsp;&nbsp;";
		}
		
	}
	
	return $linkOp;
}
function circulars_atoz(){

	db_set_active('cmsdb');
	
	
	$module_path = drupal_get_path('module', 'circulars');
		
		$op = get_circulars_header( 'atoz' );
		$op .= "<div class='result_inner'><div class='view-header'>" . Cir_atoz_letter( $sel ) . "</div>";
		$op .= "<div class='go_view'><div class='back_btn'><span>" .l(t('Back'),'circulars/atoz'). "</span></div>";
	
	/*$qry = db_select('node','n');
            $qry->addField('n','title');
			$qry->addJoin('INNER','field_data_field_not_availabilty','ca', 'n.nid = ca.entity_id');
			$qry->addJoin('INNER','field_data_field_not_upload','cf','n.nid = cf.entity_id');
			$qry->addJoin('INNER','file_usage','fu','fu.id = n.nid');
			$qry->addJoin('INNER','file_managed','fm','fm.fid = fu.fid');
			$qry->addField('fm','uri');
			$qry->addField('fm','filesize');
            $qry->condition('n.status', '1');
			
	//print $qry;
	$cir_result = $qry->extend('PagerDefault')->limit(20)->execute();
	db_set_active('default');
	
	foreach($cir_result as $res){
	
		 
		$filepath = $res->uri;
		$filesize = $res->filesize;
		$title = $res->title;
				
	if(empty($filepath)){		
							$wrapper = file_stream_wrapper_get_instance_by_uri($res->uri);
                               
							if($wrapper == true){
								$filepath = $wrapper->getDirectoryPath() . "/" . file_uri_target($res->uri);
							}
							$icon = '';
					}else{
						  	$icon = get_pdf_icon($filesize)	; 
						 	//$filepath = $baseurl.$contentlist->filepath;	
							$wrapper = file_stream_wrapper_get_instance_by_uri($res->uri);
                               
							if($wrapper == true){
								$filepath = $wrapper->getDirectoryPath() . "/" . file_uri_target($res->uri);
								
							}//$department= $contentlist -> dept_name;	
						  
					}
					
					if($filesize != 0){
						
							$kb = ceil($res->filesize/1024);
							$kbyte= $kb.KB;
							if($kb >1024) { 
							$kb = ceil($kb/1024);
							$kbyte= $kb.MB;
						}
					}
		
		/*if( $goYear != $go_year )		{ 
					$go_year = $goYear;
					$divOutput1 = "<div class='res_dept go_depart' ><strong>Year : " .  $goYear  . "</strong></div>";
				}
				else		{
					$divOutput1 = '';
				}*/
		/*$op .= "<div class='go_view_dept_list'> <p>
				
				<a target='_blank' href='http://cms.tn.gov.in/tncms_latest/" .$filepath. "'>"
				.$title ."</a>" . $icon. $kbyte. "</p>
				  
			</div>";*/
			
				$query = new EntityFieldQuery();
				$query->entityCondition('entity_type', 'node')
					->entityCondition('bundle', 'notification')
					->propertyCondition('status',1)
					->fieldOrderBy('field_not_department', 'value', 'DESC')
					->pager(25,0); 
			
				$result = $query->execute();
				 
				$nid = current($result['node'])->nid;
				 
				if (isset($result['node'])) {
					$news_items_nids = array_keys($result['node']);
					//$news_items = entity_load('node', $news_items_nids);
					$news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
				}
			
		db_set_active('default');
		
		foreach($news_items_nids as $nid) {
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$res = node_load($nid, $vid);
			my_db_change(0);
		 
		// print "<pre>"; print_r($res); print "</pre>";
			
		
		$filepath = $res->field_not_upload['und'][0]['uri'];
		$filesize = $res->field_not_upload['und'][0]['filesize'];
		$title = $res->title;
		if ($res->field_not_upload['und'][0]['filesize'] != '0') {
						$kb = ceil($res->field_not_upload['und'][0]['filesize']/1024);
						$kbyte= $kb.KB;
						if($kb >1024) { 
							$kb = ceil($kb/1024);
							$kbyte= $kb.MB;
						}
					}
		if($res->field_not_availabilty['und'][0]['value'] == 'download')  {
				 $link = $res->field_not_upload['und'][0]['uri'];		
				$wrapper = file_stream_wrapper_get_instance_by_uri($link);
				if($wrapper == true){
					 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
				}
			 	$filepath="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
				$icon = '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">'; 
				$icon = $icon. $kbyte;
			}
			else  {
				$filepath = $res->field_not_url['und'][0]['value'];
				$filepath ="'$filepath'";
				$icon = ''	; 
			}
 
	
	$op1 .= '<div class="go_view_dept_list"> <p>
				
				<a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepath.');">'.ucfirst($title) .'</a>' . $icon. '</p>
				  
			</div>';
			
	}
	$output .= $op;
	if($res){
		$output .= $op1;
	}else {
		$output .='<div class="no_record">No Results Found</div>';
	}
	
		//$output .= "<div class='clr'></div></div>";	
		
		return $output.theme('pager');
			
	
	
	}
	
function circulars_atoz_letter(){

	 $letter_val = arg(2);

	db_set_active('cmsdb');
	
	
	$module_path = drupal_get_path('module', 'circulars');
		
		$op = get_circulars_header( 'atoz' );
		$op .= "<div class='result_inner'><div class='view-header'>" . Cir_atoz_letter( $sel ) . "</div>";
		$op .= "<div class='go_view'><div class='back_btn'><span>" .l(t('Back'),'circulars/atoz'). "</span></div>";
	
	if($letter_val !='All') {
	/*$qry = db_select('node','n');
            $qry->addField('n','title');
			$qry->addJoin('INNER','field_data_field_not_availabilty','ca', 'n.nid = ca.entity_id');
			$qry->addJoin('INNER','field_data_field_not_upload','cf','n.nid = cf.entity_id');
			$qry->addJoin('INNER','file_usage','fu','fu.id = n.nid');
			$qry->addJoin('INNER','file_managed','fm','fm.fid = fu.fid');
			$qry->addField('fm','uri');
			$qry->addField('fm','filesize');
            $qry->condition('n.status', '1');
			$qry->condition('n.title', $letter_val.'%','LIKE');*/
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'notification')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$letter_val.'%','LIKE')
			//  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 

	}
	else{
		
		/*$qry = db_select('node','n');
            $qry->addField('n','title');
			$qry->addJoin('INNER','field_data_field_not_availabilty','ca', 'n.nid = ca.entity_id');
			$qry->addJoin('INNER','field_data_field_not_upload','cf','n.nid = cf.entity_id');
			$qry->addJoin('INNER','file_usage','fu','fu.id = n.nid');
			$qry->addJoin('INNER','file_managed','fm','fm.fid = fu.fid');
			$qry->addField('fm','uri');
			$qry->addField('fm','filesize');
            $qry->condition('n.status', '1');*/
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'notification')   
			  ->propertyCondition('status',1)
			//  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 

	}		
	
	
	$result = $query->execute();
				 
				$nid = current($result['node'])->nid;
				 
				if (isset($result['node'])) {
					$news_items_nids = array_keys($result['node']);
					//$news_items = entity_load('node', $news_items_nids);
					$news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
				}
			
		db_set_active('default');
		
		foreach($news_items_nids as $nid) {
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$res = node_load($nid, $vid);
			my_db_change(0);
		 
		// print "<pre>"; print_r($res); print "</pre>";
			
		
		$filepath = $res->field_not_upload['und'][0]['uri'];
		$filesize = $res->field_not_upload['und'][0]['filesize'];
		$title = $res->title;
		if ($res->field_not_upload['und'][0]['filesize'] != '0') {
						$kb = ceil($res->field_not_upload['und'][0]['filesize']/1024);
						$kbyte= $kb.KB;
						if($kb >1024) { 
							$kb = ceil($kb/1024);
							$kbyte= $kb.MB;
						}
					}
		if($res->field_not_availabilty['und'][0]['value'] == 'download')  {
				 $link = $res->field_not_upload['und'][0]['uri'];		
				$wrapper = file_stream_wrapper_get_instance_by_uri($link);
				if($wrapper == true){
					 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
				}
			 	$filepath="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
				$icon = '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">'; 
				$icon = $icon. $kbyte;
			}
			else  {
				$filepath = $res->field_not_url['und'][0]['value'];
				$filepath ="'$filepath'";
				$icon = ''	; 
			}
 
	
	$op1 .= '<div class="go_view_dept_list"> <p>
				
				<a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepath.');">'.ucfirst($title) .'</a>' . $icon. '</p>
				  
			</div>';
			
	}
	$output .= $op;
	if($res){
		$output .= $op1;
	}else {
		$output .='<div class="no_record">No Results Found</div>';
	}
	
		//$output .= "<div class='clr'></div></div>";	
		
		return $output.theme('pager');
}

function circular_search_form($form_state){

	$keyword = trim(arg(2));
	$radval = trim(arg(3));
	$cir_year = trim(arg(4));
	
	$form['cir_search'] = array(
	'#type' => 'textfield',
	'#title' => t('Keyword'),
	'#default_value' => $keyword ? $keyword : '',
	'#size'=>'35',
	'#prefix' => '<div class="keyword_go">',
	
	); 
	
	 $form[ 'cir_submit' ] = array(
	'#title' => 'Submit', 
	'#type' => 'submit', 
	'#value' => 'Go', 
	'#name' => 'search',
	'#prefix' => '<div class="sea_go_but">',
	'#suffix' => '</div></div>',
	
	);
	  
	$options = array(
  		'cir_title'=>t('Tittle'),
		'cir_num'=>t('Number')
	);
	$form['rad'] = array(
	'#type' => 'radios',
	'#default_value' => arg(3) ? arg(3) : 'cir_title',
	'#options' => $options,
	'#required' => TRUE,
	//'#default_value' =>  'cir_num' ? 'cir_num' : 'cir_title',
	//'#default_value' => isset($rad) ? $rad : false,
	);
	
	$options = array(
		''=>'Select Year',
  		'1970-1990'=>t('1970-1990'),
		'1991-2000'=>t('1991-2000'),
		'2001-2010'=>t('2001-2010'),
		'2011-2020'=>t('2011-2020')
	);
	$form['cir_year'] = array(
	'#type' => 'select',
	'#default_value' => $cir_year ? $cir_year : '',
	'#options' => $options,
	//'#default_value' =>  $options['Tittle'],
	);
	
	
	
	return $form;
}

function circulars_search(){

	drupal_set_title(t('Government of Tamil Nadu')." : ".t('Circulars and notification'));
	$output .= ''; 	
	//unset_search_session('cir_keyword');
	//variable_set('go_search_word', '');	
	$docheader = get_circulars_header( '' );
	$docheader = get_circulars_header( 'find' ) ."</div>";
	$StrOp = "<div class='result_inner' id='result_go'><div class='go_view'>";
	$StrEnd = "<div class='clr'></div></div><div class='clr'></div>";
	$output =  $docheader.$StrOp . drupal_render(drupal_get_form('circular_search_form')).$StrEnd;
	return $output;
}

function circular_search_form_submit($form,&$form_state){
	global $base_url;	
	
	//print "form submit";  
	 
	 //print "<pre>"; print_r($form_state); print "</pre>";
	 //drupal_set_message(t('The form has been submitted.')); die;
	 
	 	if ($form_state[clicked_button]['#name'] == 'search') {		
			
			if($form_state[values][cir_search]!=""){
	   
			   $cir_search = $form_state[values][cir_search]; 
			   } 
			   
			   else 
			   {
				  $cir_search = "0";
				  if($cir_search == '' || $cir_search == '0'){form_set_error('cir_search','Please Enter Key Word');}
				 
			   }
			
		
		if( $form_state[values][rad]!="" ){
	   
	   		$rad =  $form_state[values][rad];
	   	} 
	   
	   else {
	   	      $rad = "NULL";
		  //$form_state['redirect'] = 'circulars/simple_search/'.$cir_search."/".$rad_cir."/".$search_year;
	   }
	   if( $form_state[values][cir_year]!=""){
	   
	   	$cir_year = $form_state[values][cir_year];
	   } 
	   
	   else 
	   {
	      $cir_year = "NULL";
	   }
		
	}		
	$form_state['redirect'] = 'circulars/searchresult/'.$cir_search."/".$rad."/".$cir_year; 
    return;
}

function circulars_search_result(){

	

	$docheader =  get_circulars_header( 'find' ) ."</div>";
		//$op = get_govt_header( '' );
		$docheader .= drupal_render(drupal_get_form('cir_search'));

	//$op .= "<div class='go_view'><div class='back_btn'><span>" .l(t('Back'),'go_view/dept'). "</span></div>";	
		$str = strtoupper(trim(arg(2))); 
		
					
		$cir_keyword= trim(arg(2)); 
		$cir_keyword= strtolower($cir_keyword);	
		//$ser_string = array();
		$cir_keyword = preg_replace('/[\p{Z}\s]{2,}/u', ' ', $cir_keyword);
		$cir_keyword=pg_escape_string($cir_keyword); 
		
		$len = drupal_strlen($cir_keyword);
		$keyword1 =$cir_keyword; 
		
		
			// Do not count UTF-8 continuation bytes.
		 if(!empty($keyword1))
		 {
		$position[0]=strpos($keyword1,'"');
		
		 if($position[0] != "");{
		
		  $position[1]=strpos($keyword1,'"',$position[0]+1);
		
		  }
		  }
		 if(!empty($position[1]))
		 {
			//echo substr($keyword,1)."</br>";
			$k=0;
			for($i=0;$i<=$len;$i++)
			{
			
			$position[0]=strpos($keyword1,'"');
			
			if(strlen($keyword1) !=0)
			{
			if($position[0] !=0 && $position[0] !="")
			$str_not_quote[$k++] =substr($keyword1,0,$position[0]);
			if($position[0] ==false)
			$str_not_quote[$k++] =substr($keyword1,0,strlen($keyword1));
			}
			
			
			 $position[1]=strpos($keyword1,'"',$position[0]+1);
			
			 if(empty($position[1]))
			 {
			 break;
			 }
			 $sub_string_len = $position[1] - $position[0]-1;
			 
		 $ser_string[$i] = substr($keyword1,$position[0]+1, $sub_string_len);
			
			 $part_string= substr($keyword1,$position[1]+1, $len);
			
			$keyword1 =trim($part_string);
			if(empty($keyword1))
			{
			break;
			}
			}
			}
			$ser_string = array_filter_customize($ser_string);
			//print_r($ser_string);
			if(!empty($ser_string))
			{
			
				$sql_search_clause = search_keyword('LOWER(n.title)', $ser_string, $str_not_quote);
				$sql_search_clause .= "or ";
				$sql_search_clause .= search_keyword('LOWER(g.field_go_abstract_value)', $ser_string, $str_not_quote);
			}

	if($cir_keyword==''){ 		
		$op = "<div class ='go_box'><strong>" .'Please Enter The Keyword'. "</strong></div>" ;
		$docheader = get_circulars_header( '' );
		$StrOp = "<div class='result_inner' id='result_go'><div class='go_view'>";
		$StrEnd = "<div class='clr'></div></div><div class='clr'></div></div>";
		$output =  $docheader.$StrOp . drupal_render(drupal_get_form('circular_search_form')).$op.$StrEnd;
		return $output;
	}
	$cir_val = $cir_keyword; 
	
	
	//if($cir_search !='NULL' && $rad != 'NULL'){
	
		db_set_active('cmsdb');
		
		$cir_val = trim(arg(2));
		if(arg(3) == 'cir_title'){
			$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->propertyCondition('title','%'. $cir_val.'%','LIKE')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC')
					  ->pager(15,0); 
			$result = $query->execute();	
			$nid = current($result['node']);
			
			if (isset($result['node'])) {
			  $news_items_nids = array_keys($result['node']); 	  
			 $news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
			}
		$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->propertyCondition('title','%'. $cir_val.'%','LIKE')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC');
					 $row_count = $query->count()->execute();

        }
		if(arg(3) == 'cir_num'){
		
			$cir_val = trim(arg(2));
			$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->fieldCondition('field_not_number', 'value',$cir_val,'=')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC')
					  ->pager(15,0); 
			$result = $query->execute();	
			$nid = current($result['node']);
			
			if (isset($result['node'])) {
			  $news_items_nids = array_keys($result['node']); 	  
			 $news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
			}
			$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->fieldCondition('field_not_number','value', $cir_val,'=')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC');
					   $row_count = $query->count()->execute();
	}
	
 //}
 
 if($cir_year!='NULL' && (arg(3) =='NULL' && arg(2) !='NULL') ) { 
 
 	$cir_year = arg(4);
 	$split_year = split('-',$cir_year);
    $year1=$split_year['0'];
    $year2=$split_year['1'];
	$cir_val = trim(arg(2));
	
	db_set_active('cmsdb');
	$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->propertyCondition('title','%'. $cir_val.'%','LIKE')
					  ->fieldCondition('field_not_year_value',array($year1, $year2),'BETWEEN')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC')
					  ->pager(15,0); 
			$result = $query->execute();	
			$nid = current($result['node']);
			
			if (isset($result['node'])) {
			  $news_items_nids = array_keys($result['node']); 	  
			 $news_items = node_load_multiple($news_items_nids, array('type' => 'notification'));
			}
			$query = new EntityFieldQuery();
					$query->entityCondition('entity_type', 'node')
					  ->entityCondition('bundle', 'notification')   
					  ->propertyCondition('status',1)
					  ->propertyCondition('title','%'. $cir_val.'%','LIKE')
					  ->fieldCondition('field_not_year','value',array($year1, $year2),'BETWEEN')
					  //->propertyCondition('language', $language->language, '=')
					  ->propertyOrderBy('title','ASC');
					   $row_count = $query->count()->execute();
 
 }

	db_set_active('default');
	
	if($row_count==''){
		$val = "<div class ='go_box'><strong>" .'No record Found'."</strong></div>";
	}
	else{
		$val = "<div class ='go_box'><strong>" .$row_count .'  record Found' . "</strong></div>";
	}
foreach($news_items_nids as $nid) {
	my_db_change(1);
	$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
	$res = node_load($nid, $vid);
	my_db_change(0);
		 
		//print "<pre>"; print_r($res); print "</pre>";
			
		
		$filepath = $res->field_not_upload['und'][0]['uri'];
		$filesize = $res->field_not_upload['und'][0]['filesize'];
		$title = $res->title;
		if ($res->field_not_upload['und'][0]['filesize'] != '0') {
						$kb = ceil($res->field_not_upload['und'][0]['filesize']/1024);
						$kbyte= $kb.KB;
						if($kb >1024) { 
							$kb = ceil($kb/1024);
							$kbyte= $kb.MB;
						}
					}
		if($res->field_not_availabilty['und'][0]['value'] == 'download')  {
				 $link = $res->field_not_upload['und'][0]['uri'];		
				$wrapper = file_stream_wrapper_get_instance_by_uri($link);
				if($wrapper == true){
					 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
				}
			 	$filepath="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
				$icon = '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">'; 
				$icon = $icon. $kbyte;
			}
			else  {
				$filepath = $res->field_not_url['und'][0]['value'];
				$filepath ="'$filepath'";
				$icon = ''	; 
			}
 
	
	$op1 .= '<div class="go_view_dept_list"> <p>
				
				<a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepath.');">'.ucfirst($title) .'</a>' . $icon. '</p>
				  
			</div>';
			
	}
		$head = get_circulars_header( '' );
		$head1 = drupal_render(drupal_get_form('circular_search_form'));
		$StrOp = "<div class='result_inner' id='result_go'><div class='go_view'>";
		$StrEnd = "<div class='clr'></div></div><div class='clr'></div></div>";
		$output .= $head. $StrOp.$head1.$val.$op.$op1.$StrEnd;
	
	return $output.theme('pager');
}

function get_circulars_header($css )
{	

	global $base_url;
	 if($css=='atoz'){ $atoz= "view_head_sel";}else {$atoz= "view_head_nonsel";}
     if($css=='dept') { $dept="view_head_sel"; }else{ $dept='view_head_nonsel'; }
     if($css=='find') { $find="view_head_sel"; }else {$find='view_head_nonsel'; }
	  
	   
	global $language;
	if($language->language=='ta'){
		$css='cir_interest_ta';
		
		$html = "<div class='view-header' id='viewmod_$css'>
		<div class='inner_tab_content'>
		<div class='inner_li'>".l(t('Department wise'),'ta/circulars/deptname')."</div>
		<div class='inner_li'>".l(t('Search'),'ta/circulars/search')."</div>
		<div class='inner_li'>".l(t('A to Z'),'ta/circulars/atoz')."</div>
		<div class='clr'></div></div>
	</div>";
		
	}
	else{
		$css='cir_interest';
	

	$html = "<div class='view-header' id='viewmod_$css'>
		<div class='inner_tab_content'>
		<div class='inner_li'>".l(t('Department wise'),'circulars/deptname')."</div>
		<div class='inner_li'>".l(t('Search'),'circulars/search')."</div>
		<div class='inner_li'>".l(t('A to Z'),'circulars/atoz')."</div>
		<div class='clr'></div></div>
	</div>";
	}
	return $html;
}

function search_keyword($colum_name, $ser_string, $str_not_quote)
	{
	$i=0;
	foreach($ser_string as $key => $value)
	{
	
	if($i==0)
	$ser_keywords .= $colum_name." like '%%".$value."%' ";
	else
	$ser_keywords .= "and ".$colum_name." like '%%".$value."%' ";
	$i++;
	
	}
	$i=0;
	foreach($str_not_quote as $key => $value)
	{
	
	if($i==0)
	$ser_keywords_not_quote .= $colum_name." like '%%".$value."%' ";
	else
	$ser_keywords_not_quote .= "or ".$colum_name." like '%%".$value."%' ";
	$i++;
	
	}
	return "((".$ser_keywords.") or (".$ser_keywords_not_quote."))";
	}

?>